package cn.icanci.ladybird.service.user.impl;

import cn.icanci.ladybird.dao.UserBuyerTicketDao;
import cn.icanci.ladybird.pojo.dto.UserBuyerTicketDTO;
import cn.icanci.ladybird.pojo.entity.BuyerTicket;
import cn.icanci.ladybird.pojo.entity.UserInfo;
import cn.icanci.ladybird.pojo.entity.UserInfoBuyerTicketMiddle;
import cn.icanci.ladybird.service.BuyerTicketService;
import cn.icanci.ladybird.service.UserInfoBuyerTicketMiddleService;
import cn.icanci.ladybird.service.UserInfoService;
import cn.icanci.ladybird.service.user.UserBuyerTicketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zxp
 * @date: Created in 2020/10/3 0003 下午 19:16
 * @classAction: 常用购票人
 */
@SuppressWarnings("all")
@Service
public class UserBuyerTicketServiceImpl implements UserBuyerTicketService {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserInfoBuyerTicketMiddleService userInfoBuyerTicketMiddleService;
    @Autowired
    private BuyerTicketService buyerTicketService;
    @Autowired
    private UserBuyerTicketDao userBuyerTicketDao;


    @Override
    public List<UserBuyerTicketDTO> userBuyerTicketDTO() {
        // 根据用户的id查询所有的购票人
        List<UserInfoBuyerTicketMiddle> ticketMiddles = userInfoBuyerTicketMiddleService.queryByUserInfoId(1L);
//        List<UserInfoBuyerTicketMiddle> ticketMiddles = userInfoBuyerTicketMiddleService.queryAllByLimit(0,8);
        List<UserBuyerTicketDTO>  list =  new ArrayList<UserBuyerTicketDTO>();
        for (UserInfoBuyerTicketMiddle ticketMiddle:ticketMiddles) {
            Long userId = ticketMiddle.getBuyerTicketMiddleId();
            BuyerTicket buyerTicket = buyerTicketService.queryById(userId);
            // 查询用户的信息
            UserBuyerTicketDTO userBuyerTicketDTO = new UserBuyerTicketDTO();
            userBuyerTicketDTO.setUserInfoId(1L);
            //设置证件类型
            userBuyerTicketDTO.setBuyerTicketDocumentType(buyerTicket.getBuyerTicketDocumentType());
            //设置购票人姓名
            userBuyerTicketDTO.setBuyerTicketUsername(buyerTicket.getBuyerTicketUsername());
            //设置证件号
            userBuyerTicketDTO.setBuyerTicketIdCardNumber(buyerTicket.getBuyerTicketIdCardNumber());
            //设置购票人id
            userBuyerTicketDTO.setBuyerTicketId(buyerTicket.getBuyerTicketId());
            list.add(userBuyerTicketDTO);
        }

        return list;
    }

    @Override
    public PageInfo<UserBuyerTicketDTO> queryAllByNotObject(Integer pageNum, Integer pageSize) {
        // 分页
        // pageNum：当前页数   pageSize：当前页需要显示的数量
        // 只要有一个没有传值，那么默认就是首页，然后设置为首页
        if (null == pageNum || null == pageSize) {
            pageNum = 1;
            pageSize = 6;
        }
        // 写在查询的前面
        PageHelper.startPage(pageNum, pageSize);
        List<UserBuyerTicketDTO> userBuyerTicketDTOS = this.userBuyerTicketDao.queryAllByNotObject();
        // 把查询的结果封装到 PageInfo
        PageInfo<UserBuyerTicketDTO>  userBuyerTicketDTOPageInfo = new PageInfo<>(userBuyerTicketDTOS);
        return userBuyerTicketDTOPageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long buyTicketId) {
        UserInfoBuyerTicketMiddle userInfoBuyerTicketMiddle = userInfoBuyerTicketMiddleService.queryByTicketId(buyTicketId);
        userInfoBuyerTicketMiddleService.deleteByTicketId(buyTicketId);
        buyerTicketService.deleteById(buyTicketId);
    }
}
